﻿// visitor class that serializes an AAATrip object
MapControl2.AAAJsonSerializerVisitor = function(){
    this._jsonTrip = null;
    this._jsonRoutes = new Array();
    this._currentRoute = -1;
};

MapControl2.AAAJsonSerializerVisitor.prototype = {

    // geoloc json serialization
    _serializeGeoLoc: function(geoloc)
    {
        var addr = "null";
        if (geoloc.address != null)
            addr = geoloc.address;
       
        var descr = "null";
        if (geoloc.description != null)
            descr = geoloc.description;
        

        
        var json = '{"lat":'             + geoloc.lat.toFixed(5)        + ', ' +
                     '"lng":'             + geoloc.lng.toFixed(5)        + ', ' +
                     '"address":"'       + addr    + '", ' +
                     '"description":"'   + descr   + '", ' +
                     '"gettingInUsers":[' + ((geoloc.gettingInUsers != null && geoloc.gettingInUsers.length>0)?geoloc.gettingInUsers.toString() + '], ': '], ')  + 
                     '"gettingOffUsers":[' + ((geoloc.gettingOffUsers != null && geoloc.gettingOffUsers.length>0)?geoloc.gettingOffUsers.toString()+ ']}': ']}');

    
        return json;                 
    },
    
    // geoloc array serialization function
    _serializeGeoLocArray: function(geoloc_array)
    {
        if (geoloc_array == null)
        {
            return '[]';
        }else
        {
            var toRet = '[';
            var i = 0;
            
            for(i = 0; i<geoloc_array.length; i++)
            {
                if (i > 0)
                {
                    toRet += ', ';
                }
                toRet += this._serializeGeoLoc(geoloc_array[i]);
            }
            toRet += ']';
            return toRet;
        }
    },
    
    // GPolyline json serialization function
    _serializePolyline: function(polyline)
    {
        var jsonPolyline = '{"levels":"'+polyline[_googleEncPol].levels+ '"'+
                           ',"numLevels":'+polyline[_googleEncPol].numLevels +
                           ',"zoomFactor":' + polyline[_googleEncPol].zoomFactor;
        var pointsStr = polyline[_googleEncPol].points;
        pointsStr = pointsStr.replace(/\\/g,"\\\\");
        jsonPolyline += ',"points":"' + pointsStr + '"}';
        return jsonPolyline;
    },

    visit: function(element, element_type)
    {
        switch(element_type)
        {
            // AAATrip json serialization
            case MapControl2.AAATRIP_ELEMENT:
                
                var trip = element.getTripModel();
                var i = 0;
                var routes = element.getRoutes();
                this._currentRoute = 0;
                
                // first we serialize the AAARoute objects...
                for(i = 0; i<routes.length; i++)
                {
                    routes[i].accept(this);
                    this._currentRoute++;
                }
                
                // ...then we serialize the other simple fields...
                var simpleFields = '"id":' + trip.id + ','+
                                   '"CoveredDistance":' + trip.CoveredDistance + ', ' +
                                   '"SpentTime":' + trip.SpentTime + ', ' + 
                                   '"deviationRadius":' + trip.deviationRadius + ', ';
                
                // ...we complete route serialization...
                var jsonRoutes = '"routes":[';
                for (i = 0; i< routes.length; i++)
                {
                    if (i > 0)
                    {
                        jsonRoutes += ', ';
                    }    
                    jsonRoutes += this._jsonRoutes[i];
                }
                jsonRoutes += '],';
                
                // ...we serialize the GPolyline objects of the routes...
                var polylines = '"GPolyline":[';
                for(i = 0; i<routes.length; i++)
                {
                    if(i!=0) polylines += ',';
                    polylines += this._serializePolyline(routes[i].getPolyline());
                }                                   
                polylines += ']';                
                
                // ...finally we put all together.
                this._jsonTrip = '{' + simpleFields + jsonRoutes + polylines + '}';
            break;
            
            case MapControl2.AAAROUTE_ELEMENT: // AAARoute object serialization
                
                var route = element.getRouteModel();
                
                // some simple field json serialization...
                var jsonRoute = '{"consistent":true' +
                    ',"distance":' + route.distance +
                    ',"id":' + route.id + 
                    ',"passengers":1' +
                    ',"time":' + route.time + 
                    ',"vehicle":' + route.vehicle +
                    ',"vehicleId":' + route.vehicleId;
                    
                // ...start and end serialization...
                var jsonStart = this._serializeGeoLoc(route.start);
                var jsonEnd = this._serializeGeoLoc(route.end);
                
                jsonRoute += ',"start":' + jsonStart +
                             ',"end":' + jsonEnd; 
                
                // ...waypoints and driving instructions serialization...
                var jsonWaypoints = this._serializeGeoLocArray(route.viaPoints);
                var jsonInstructions = this._serializeGeoLocArray(route.instructions);
                jsonRoute += ',"viaPoints":' + jsonWaypoints +
                             ',"instructions":[]';  //' + jsonInstructions + ', ';
                
                // ...polyline serialization (unused field)...
                jsonRoute += ',"polyline":[]}';   // + jsonPolyline + '}';
                
                // ...finally we store the json string.
                this._jsonRoutes[this._currentRoute] = jsonRoute;
            break;
        }
    },
    
    getResult: function()
    {
        return this._jsonTrip;
    }
};

MapControl2.AAAJsonSerializerVisitor.registerClass
    ('MapControl2.AAAJsonSerializerVisitor');
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();